package require http

namespace eval ::tclers_chat {

    proc m { mt mn mm {pn ""} {type groupchat} } {
	Wubchain::m $mt $mn $mm $pn $type
    }

    proc load_history { NICK } {
	exec [info nameofexecutable] get_history.tcl
	set f [open history.dat r]
	set txt [read $f]
	close $f
	eval $txt
    }

    proc create_nick_css { } {
	set wubchain_nick_css_list {}
	exec [info nameofexecutable] get_nick_css.tcl
	set f [open nickcss.dat r]
	set txt [read $f]
	close $f
	set wubchain_nick_css ""
	set ll {}
	foreach l [split $txt \n] {
	    if { [regexp {^span\.(\w+)\s+\{\s*color:\s*(#[[:xdigit:]]+)\s*;\s*\}$} $l m nick color] } {
		lappend wubchain_nick_css_list [list $nick $color]
		lappend ll $l
	    }
	}
	append wubchain_nick_css [join $ll \n]
	set f [open $::docroot/css/wubchain_nick.css w]
	puts $f $wubchain_nick_css
	close $f
	return $wubchain_nick_css_list
    }

    proc tip_regexp { txt } {
	return [regexp -inline -all -indices -- {/?[tT][Ii][Pp]\s*(\d+)} $txt]
    }
    
    proc make_tipref {tip} {
	regexp {\s*/?[Tt][Ii][Pp]\s*(\d+)} $tip -> ntip
	return [<a> href http://www.tcl.tk/cgi-bin/tct/tip/$ntip target _blank "$tip"]
    }

    proc redirect_tip {r MSG} {
	if {[regexp {^\s*/[Tt][Ii][Pp]\s*(\d+)$} $MSG -> tip]} {
	    return "http://www.tcl.tk/cgi-bin/tct/tip/$tip"
	}	""
	return 
    }

    proc sfbug_regexp { txt } { 
	return [regexp -inline -all -indices -- {(\d{6,7})} $txt]
    }
    
    proc make_sfbugref {id} {
	return [<a> href http://sourceforge.net/support/tracker.php?aid=$id target _blank $id]
    }

    proc redirect_sfbug {r MSG} {
	if {[regexp {^\s*/[Bb][Uu][Gg]\s*(\d+)$} $MSG -> bug]} {
	    return "http://sourceforge.net/support/tracker.php?aid=$bug"
	}
	return ""
    }

    proc redirect_wiki {r MSG} {
	if {[regexp {^\s*/[Ww][Ii][Kk][Ii]\s*(.+)$} $MSG -> page]} {
	    return "http://wiki.tcl.tk/$page"
	}
	return ""
    }

    proc redirect_help { } {
	set h ""
	append H [<dt> "/wiki [<i> {page_number or search_string}]"] [<dd> "Go to the Tclers's wiki and open page [<i> {page_number}] or search for [<i> {search_string}]."]
	append H [<dt> "/tip [<i> {tip_number}]"] [<dd> "Open Tcl Improvement Proposal [<i> {tip_number}]."]
	append H [<dt> "/bug [<i> {bug_id}]"] [<dd> "Open Source-Forge bug [<i> {bug_id}]."]
	return $H
    }
}
